﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.Apps.EODData.Files;
using Algobox.JuicyLib.MarketData.Structure;

namespace Algobox.JuicyLib.Apps.EODData
{
    internal class EODDataExchanges
    {
        internal EODDataExchanges(EODDataClient parent)
        {
            _eodDataClient = parent;
        }

        EODDataClient _eodDataClient;

        Dictionary<DateTime, EOD> _dataLSEEOD;
        Dictionary<DateTime, EOD> _dataMilanEOD;
        Dictionary<DateTime, EOD> _dataForexEOD;
        Dictionary<DateTime, EOD> _dataAmexEOD;
        Dictionary<DateTime, EOD> _dataOpraEOD;
        Dictionary<DateTime, EOD> _dataNyseEOD;
        Dictionary<DateTime, EOD> _dataNasdaqEOD;
        Dictionary<DateTime, EOD> _dataTSXEOD;
        Dictionary<DateTime, EOD> _dataVentureEOD;
        Dictionary<string, string> _dataNyseNames;
        Dictionary<string, string> _dataNasdaqNames;
        Dictionary<string, string> _dataAmexNames;
        Dictionary<string, string> _dataTSXNames;
        Dictionary<string, string> _dataVentureNames;

        internal Dictionary<string, OHLC> GetLSEEndOfDay(DateTime date)
        {
            if (_dataLSEEOD == null)
                _dataLSEEOD = new Dictionary<DateTime, EOD>();

            if (!_dataLSEEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_LSE_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataLSEEOD.Add(date, new EOD(ref file));
            }

            return _dataLSEEOD[date];
        }

        internal Dictionary<string, OHLC> GetMilanEndOfDay(DateTime date)
        {
            if (_dataMilanEOD == null)
                _dataMilanEOD = new Dictionary<DateTime, EOD>();

            if (!_dataMilanEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_MLSE_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataMilanEOD.Add(date, new EOD(ref file));
            }

            return _dataMilanEOD[date];
        }

        internal Dictionary<string, OHLC> GetNyseEndOfDay(DateTime date)
        {
            if (_dataNyseEOD == null)
                _dataNyseEOD = new Dictionary<DateTime, EOD>();

            if (!_dataNyseEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_NYSE_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataNyseEOD.Add(date, new EOD(ref file));
            }

            return _dataNyseEOD[date];
        }

        internal Dictionary<string, OHLC> GetNasdaqEndOfDay(DateTime date)
        {
            if (_dataNasdaqEOD == null)
                _dataNasdaqEOD = new Dictionary<DateTime, EOD>();

            if (!_dataNasdaqEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_NASDAQ_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataNasdaqEOD.Add(date, new EOD(ref file));
            }

            return _dataNasdaqEOD[date];
        }

        internal Dictionary<string, OHLC> GetAmexEndOfDay(DateTime date)
        {
            if (_dataAmexEOD == null)
                _dataAmexEOD = new Dictionary<DateTime, EOD>();

            if (!_dataAmexEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_AMEX_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataAmexEOD.Add(date, new EOD(ref file));
            }

            return _dataAmexEOD[date];
        }

        internal Dictionary<string, OHLC> GetOpraEndOfDay(DateTime date)
        {
            if (_dataOpraEOD == null)
                _dataOpraEOD = new Dictionary<DateTime, EOD>();

            if (!_dataOpraEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_OPRA_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataOpraEOD.Add(date, new EOD(ref file));
            }

            return _dataOpraEOD[date];
        }

        internal Dictionary<string, OHLC> GetForexEndOfDay(DateTime date)
        {
            if (_dataForexEOD == null)
                _dataForexEOD = new Dictionary<DateTime, EOD>();

            if (!_dataForexEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_FOREX_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataForexEOD.Add(date, new EOD(ref file));
            }

            return _dataForexEOD[date];
        }

        internal Dictionary<string, OHLC> GetVentureEndOfDay(DateTime date)
        {
            if (_dataVentureEOD == null)
                _dataVentureEOD = new Dictionary<DateTime, EOD>();

            if (!_dataVentureEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_TSXV_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataVentureEOD.Add(date, new EOD(ref file));
            }

            return _dataVentureEOD[date];
        }

        internal Dictionary<string, OHLC> GetTSXEndOfDay(DateTime date)
        {
            if (_dataTSXEOD == null)
                _dataTSXEOD = new Dictionary<DateTime, EOD>();

            if (!_dataTSXEOD.ContainsKey(date))
            {
                string file = _eodDataClient.LoadFile(String.Format(EOD.FILENAME_TSX_EOD, date));

                if (file == null || file == String.Empty)
                    return null;

                _dataTSXEOD.Add(date, new EOD(ref file));
            }

            return _dataTSXEOD[date];
        }


        internal Dictionary<string, string> GetNyseNames()
        {
            if (_dataNyseNames == null)
            {
                string file = _eodDataClient.LoadFile(Files.Names.FILENAME_NYSE);

                if (file == null || file == String.Empty)
                    return null;

                _dataNyseNames = new Names(ref file);

            }

            return _dataNyseNames;
        }
        
        internal Dictionary<string, string> GetNasdaqNames()
        {
            if (_dataNasdaqNames == null)
            {
                string file = _eodDataClient.LoadFile(Files.Names.FILENAME_NASDAQ);

                if (file == null || file == String.Empty)
                    return null;

                _dataNasdaqNames = new Names(ref file);
            }

            return _dataNasdaqNames;
        }

        internal Dictionary<string, string> GetAmexNames()
        {
            if (_dataAmexNames == null)
            {
                string file = _eodDataClient.LoadFile(Files.Names.FILENAME_AMEX);

                if (file == null || file == String.Empty)
                    return null;

                _dataAmexNames = new Names(ref file);
            }

            return _dataAmexNames;
        }

        internal Dictionary<string, string> GetVentureNames()
        {
            if (_dataVentureNames == null)
            {
                string file = _eodDataClient.LoadFile(Files.Names.FILENAME_TSXV);

                if (file == null || file == String.Empty)
                    return null;

                _dataVentureNames = new Names(ref file);
            }

            return _dataVentureNames;
        }

        internal Dictionary<string, string> GetTSXNames()
        {
            if (_dataTSXNames == null)
            {
                string file = _eodDataClient.LoadFile(Files.Names.FILENAME_TSX);

                if (file == null || file == String.Empty)
                    return null;

                _dataTSXNames = new Names(ref file);
            }

            return _dataTSXNames;
        }

    }
}
